const app = getApp();
const log =require('../../log.js');
const util=require('../../util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showMoney:true,
    status:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      let that=this;
      console.log('------mine-------');
      console.log(options);
      console.log('-------mine------');
      var parent_id=options.parent_id;
      if(parent_id!=undefined){
          this.setData({
              parent_id:parent_id
            })
            wx.setStorageSync('parent_id',parent_id);
      }else{
          parent_id = wx.getStorageSync('parent_id');
      
      }
      var open=wx.getStorageSync('open');
      console.log(open);
      var date=util.formatTime(new Date());
      log.info("Time:"+date+",page:mine,options:"+JSON.stringify(options));
      console.log(open)
      log.info("Time:"+date+",page:mine,open:"+JSON.stringify(open));
      if (open === null){
        console.log('open is null')
        wx.navigateTo({
          url: '../login/index?parent_id='+parent_id,
        })
      }
      if(open == ""){
          console.log('open is null')
          wx.navigateTo({
            url: '../login/index?parent_id='+parent_id,
          })
      }else{
          console.log('--mine----')
          console.log(open)
          console.log('----mine---')
      }
 
      that.setData({
        platform:app.globalData.platform,    
        open:open,
         width:app.globalData.windowWidth,
        height:app.globalData.windowHeight,
        statusBarHeight:app.globalData.statusBarHeight,
        navHeight:app.globalData.navHeight,
        marginTop:app.globalData.navTitleTop,
        domain:app.globalData.domain
      })
  },
  goto:function(e){
    console.log(e);
    var url=e.currentTarget.dataset.url;
    var index=e.currentTarget.dataset.index;
    var parent_id=wx.getStorageSync('parent_id');
    if(url=="#"){

    }else if(url=='phone'){
        wx.makePhoneCall({
          phoneNumber: '400-800-2177',
        })
    }else if(index=='index'){
        wx.reLaunch({
          url: url,
        })
    }else{
        wx.navigateTo({
          url: url,
        })
    }
    console.log(url);
},
goBack:function(){
  wx.navigateBack();
},
showModels:function(msg,url){
  wx.showModal({
    cancelColor: 'cancelColor',
    title:'系统提示',
    content:msg,
    //showCancel:false,
    success:function(e){
      if (e.confirm) {
         if(url!="#"){
              wx.reLaunch({
                url: url,
              })
         }
        }
    }
    

  })

},
getList:function(e){
    console.log(e);
    var status=e.currentTarget.dataset.status;
    if(status==this.data.status){

    }else{
      this.setData({
        status:status
      });
      this.getWithList();
    }

  },
  getWithList(){
    let that=this;
    var open= wx.getStorageSync('open');
    var status=this.data.status;
    wx.request({
      url:app.globalData.domain+'wechat/with/getList',    
      data: {user_id: open.id,status: status},
         success: function (res) {
           console.log(res.data);
           that.setData({
             list:res.data.list,
             moneyArray:res.data.list,
           })
        }
    });
  },
  withAll(){
    let that=this;
        var open= wx.getStorageSync('open');
        var data=this.data;
        var total=data.money;
        var idArray=[];
        var moneyArray=data.moneyArray;
        var total=0;
        moneyArray.forEach(function(item,index){             
               idArray.push(item.id);   
               total=parseFloat(total)+parseFloat(item.money)           
        })
        var ids=idArray.toString();
        if(total>2000){
            wx.showToast({
              title: '提现金额不能大于2000',
            })
        }else if(total<=0){
            wx.showToast({
                title: '提现金额为零',
              })
        }else{
            console.log('total='+total+'ids=>'+ids);
            wx.request({
                url: app.globalData.domain+'wechat/with/shareWith',
                data:{user_id:open.id,total:total,ids:ids,from:1},
                success:res=>{
                  console.log(res);
                  var data=res.data;
                  if(data.status==200){
                      that.setData({
                        total:0,
                        number:0
                      })
                    wx.showToast({
                      title: '提现成功',
                    })
                    this.getWithList();
                  }else{
                    that.setData({
                        total:0,
                        number:0
                    })
                    this.getWithList();
                    that.showModels(data.msg,data.url); 
                  }
                }
            })
        }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getWithList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})